package com.cb.domain.service.excelWork;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.cb.common.util.CommonUtil;
import com.cb.common.util.ConfigUtil;
import com.cb.domain.model.order.Policy;

public abstract class AbrExcelWorker {

	protected Document xmlDoc;
	protected POLICY_TYPE pType;
	/**
	 * EXCEL工作引擎
	 */
	protected Workbook workbook;

	protected Map<EXL_TYPE, Integer> xmlMap;

	/**
	 * XML字符串处理类
	 * 
	 * @return
	 */
	public String doWork()throws Exception {
		String xmlContent = "";
		if (null != workbook) {
			Sheet sheet = workbook.getSheet(0);
			int count = sheet.getRows();
			if (count > 0) {
				Element root = xmlDoc.addElement("PolicyList");
				root.addAttribute("username", "mysql");
				root.addAttribute("password", "mysql");
				root.addAttribute("type", pType.toString());
				for (int i = 0; i < count; i++) {

					String firstContent = sheet.getCell(0, i).getContents();
					if (StringUtils.isEmpty(firstContent)
							|| CommonUtil.strHasChinese(firstContent)) {
						continue;
					}
					if (i > 2) {
						root.addAttribute("execType", WORK_TYPE.FULL.toString());
					} else {
						root.addAttribute("execType", WORK_TYPE.ADD.toString());
					}
					createXmlStr(i, root);

				}
				xmlContent = xmlDoc.asXML();
			}
		}
		return xmlContent;
	}

	protected abstract void createXmlStr(int rowIndex, Element root);

	protected String getExlContent(EXL_TYPE type, int rowIndex) {
		String content = "";
		if (null != workbook) {
			Sheet sheet = workbook.getSheet(0);
			if (sheet.getRows() > 0) {
				if (org.apache.commons.lang.StringUtils.isNotEmpty(sheet
						.getCell(xmlMap.get(type), rowIndex).getContents())) {
					content = sheet.getCell(xmlMap.get(type), rowIndex)
							.getContents();
				}

			}
		}
		return content;
	}

	protected abstract void initMap();
	
	public List<Policy> getPolicyList(){
		List<Policy> list=new ArrayList<Policy>();
		if(xmlDoc!=null){
			Element root= xmlDoc.getRootElement();
			List<Element> eles=root.elements();
			if(null!=eles&&eles.size()>0){
				for(Element temp:eles){
					try {
						Policy policy=setPolicyInfo(temp);
						//适用航班类型
						if(policy.getFlightNumLimit().equalsIgnoreCase("所有")){
							policy.setFlightNumLimit("1");
						}
						if(policy.getFlightNumLimit().equalsIgnoreCase("不适用")){
							policy.setFlightNumLimit("2");
						}
						if(policy.getFlightNumLimit().equalsIgnoreCase("适用")){
							policy.setFlightNumLimit("3");
						}
						if(StringUtils.isNotBlank(policy.getAutoTicket())){
							policy.setAutoTicket(policy.getAutoTicket().equalsIgnoreCase("是")?"1":"0");
						}
						if(StringUtils.isNotBlank(policy.getShared())){
							policy.setShared(policy.getShared().equalsIgnoreCase("是")?"1":"0");
						}
						if(StringUtils.isNotBlank(policy.getIsSingleTrip())){
							policy.setIsSingleTrip(policy.getIsSingleTrip().equalsIgnoreCase("是")?"1":"0");
						}
						//票面价类型
						if(StringUtils.isNotBlank(policy.getDiscountType())){
							policy.setDiscountType(policy.getDiscountType().equalsIgnoreCase("指定票面价")?"4":"3");
						}
//						if(policy.getDiscountType().equalsIgnoreCase("y舱折扣")){
						
						//成本价类型
						if(StringUtils.isNotBlank(policy.getCostType())){
							policy.setCostType(policy.getCostType().equalsIgnoreCase("指定成本价")?"1":"0");
						}/*
						if(policy.getCostType().equalsIgnoreCase("指定成本价")){
							policy.setCostType("1");
						}if(policy.getCostType().equalsIgnoreCase("指定代理费率")){
							policy.setCostType("0");
						}*/
						
						if(null!=policy){
							policy.setStatus(Long.valueOf("1"));
							list.add(policy);
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			}
		}
		return list;
	}
	
	
	protected abstract Policy setPolicyInfo(Element policyItem)throws Exception;
	
	/**
	 * 初始化信息
	 * 
	 * @param filePath
	 */
	public void init(String filePath,POLICY_TYPE type) {
		this.pType=type;
		initMap();
		String web=ConfigUtil.getInstance().getXmlValByStr("user.home", "");
//		web="D:/workspace/tts/web/";
		File file = new File(web+"/"+filePath);
		try {
			xmlDoc = DocumentHelper.createDocument();
			workbook = Workbook.getWorkbook(file);
		} catch (BiffException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
